//1、Promise处理高并发问题 有 100 条数据，最多同时运行 10 个任务

alert
const tasks = Array.from({ length: 105 }, (_, i) => () =>
  new Promise(resolve => {
    const execTime = Math.random() * 1000; 
    setTimeout(() => resolve(`Task ${i} done`), execTime);
  })
);


async function limitConcurrency(tasks, limit) {
    let len =tasks.length; 
    let i = 0;
    function loopTask(){
        if(i >= len){return }
        let nowTask = tasks.slice(i,i+10);
        let pArr = nowTask.map(item=>item())
        i+=10
        Promise.all(pArr).then(res=>(
            console.log(res,'===22')
        ))
        loopTask()
        
    }
    loopTask()
}

limitConcurrency(tasks, 10).then(results => console.log(results));


// 2、求斐波那契数某一位的值，1 1 2 3 5 8 13 21...
// f(1) = 1
// f(2) = 1
// f(3) = f(2) + f(1)
// f(n) = f(n-2) + f(n-1)
function fibonacci(n) {
    let pre = 0; next = 1;
    if(n == 0) return pre;
    if(n === 1) return next;
    let cur
    for(let i=2; i<=n; i++){
        cur = pre+next;
        pre = next;
        next = cur
    }
    return cur;
}


function fibonacci2(n){
    if(n === 1 || n === 2){return 1}
    return fibonacci2(n-1)+fibonacci2(n-2)
}
console.log(fibonacci2(8))


// console.log(fibonacci(131));